#include "windows/sims.h"
#include "framelesswindow.h"
#include "darkstyle/DarkStyle.h"

#include <windows.h>

#include <QTimer>
#include <QApplication>
#include <QFileInfo>
#include <QSplashScreen>
#include <QLabel>
#include <QMovie>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    /******************启动动画设计*****************/
    QPixmap pixmap(512,220);//gif动画的尺寸大小
    QSplashScreen splash(pixmap);//利用QPixmap对象创建一个QSplashScreen对象
    splash.setWindowOpacity(0.9); // 设置窗口透明度
    QLabel label(&splash);

    //设置label大小和位置
    QMovie mv(":/res/theWanderEarth.gif");
    label.setMovie(&mv);
    mv.start();
    //显示此启动图片
    splash.show();
    splash.setCursor(Qt::BlankCursor);
    for (int i = 0; i < 3000; i += mv.speed()) {
      a.processEvents(); //使程序在显示启动画面的同时仍能响应鼠标等其他事件
      Sleep(mv.speed()); // 延时
    }
    /******************启动动画设计*****************/


    QApplication::setStyle(new DarkStyle);

    SIMS w;
    FramelessWindow framelessWindow;
    QIcon Icon(QCoreApplication::applicationDirPath()+"/res/icon.png");

    framelessWindow.setWindowTitle("SIMS by LRC");
    framelessWindow.setWindowIcon(Icon);
    framelessWindow.setContent(&w);
    framelessWindow.show();

    splash.finish(&framelessWindow); //在主体对象初始化完成后结束启动动画

#ifdef QT_DEBUG
    /* 实时更新样式 */
    QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, []() {
        static QDateTime dateTime;
//        QFileInfo fileInfo(":/res/style.css");
        QFileInfo fileInfo(QCoreApplication::applicationDirPath()+"/res/style.css");
        if (fileInfo.exists())
        {
            QDateTime fileTime = fileInfo.lastModified();
            if (dateTime == fileTime)
                return;
            dateTime = fileTime;

//            QFile file(":/res/style.css");
            QFile file(QCoreApplication::applicationDirPath()+"/res/style.css");
            if (file.open(QIODevice::ReadOnly))
            {
                qobject_cast<QApplication*>(QApplication::instance())->setStyleSheet(file.readAll());
                file.close();
            }
        }
    });
    timer.start(33);
#endif

    return a.exec();
}

